<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\activity;

use Biz\tools\ShopArea;


class FullShippingPlan extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'full_shipping_plan';


    //状态
    const STATUS_ACTIVE = 1;//正常
    const STATUS_INVALID = 0; // 失效
    const STATUS_DELETE = 9;//删除状态


    public static function getActiveList($shippingIds, $status = self::STATUS_ACTIVE, $sort = null)
    {
        $formatList = [];

        $where['shipping_id'] = $shippingIds;
        if (!is_null($status)) {
            $where['status'] = $status;
        }
        if ($sort) {
            $where['ORDER'] = $sort;
        }
        $list = self::select('*', $where);

        if (empty($list)) {
            return $formatList;
        }


        foreach ($list as $info) {
            $formatList[$info['shop_id']][] = [
                'id' => $info['id'],
                'shipping_id' => $info['shipping_id'],
                'total_unit' => $info['total_unit'],
                'province_ids' => $info['province_ids'],
                'province_desc' => ShopArea::locationId2String($info['province_ids'])
            ];
        }

        return $formatList;
    }


    public static function getInfos($shippingIds)
    {
        $planInfos = self::select("*", [
            'shipping_id' => $shippingIds,
            'status' => self::STATUS_ACTIVE,
        ]);

        self::formatInfos($planInfos);

        $rst = [];

        foreach ($planInfos as $planInfo) {
            $rst[$planInfo['shipping_id']][] = $planInfo;
        }

        return $rst;
    }

    private static function formatInfos(&$planList)
    {
        foreach ($planList as &$plan) {
            $plan['province_desc'] = ShopArea::locationId2String($plan['province_ids']);
            $plan['province_ids'] = explode(',', $plan['province_ids']);
        }

    }

    public static function addData($shop_id, $plan_id, array $fields = [])
    {
        $data = array_map(function ($field) use ($shop_id, $plan_id) {
            $data = [];
            if ($field['province_ids']) {
                $data['shop_id'] = $shop_id;
                $data['shipping_id'] = $plan_id;
                $data['province_ids'] = $field['province_ids'];
                $data['total_unit'] = $field['total_unit'];
                $data['status'] = self::STATUS_ACTIVE;
            }
            return $data;
        }, $fields);
        $ret = self::insert(array_filter($data));
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;

    }

    public static function editPlan($id, $data)
    {
        if (isset($data['province_ids']) && !$data['province_ids']) {
            $data = [];
            $data['status'] = self::STATUS_DELETE;
        }
        $data['edittime'] = date('Y-m-d H:i:s');
        $ret = self::update($data, [
            'id' => $id,
        ]);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function updateData($data, $where)
    {
        $data['edittime'] = date('Y-m-d H:i:s');
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

}